package com.example.test1.controller;

import com.example.test1.Dto.CommentDTO;
import com.example.test1.Dto.NumDTO;
import com.example.test1.Dto.ResponseDTO;
import com.example.test1.service.ReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping(value = "/read", produces = "application/json")
public class ReadController {
    @Autowired
    private ReadService readService;

    /**
     * 获取短文
     */

    @RequestMapping(value = "/text", method = RequestMethod.POST)
    public ResponseDTO<Object> text(@RequestBody NumDTO numDto) {
        Map<String, String> content = readService.getText(numDto.getNum());
        return new ResponseDTO<>("200", "success", content);
    }

    /**
     * 获取随机一组短文
     */

    @RequestMapping(value = "/randomText", method = RequestMethod.POST)
    public ResponseDTO<Object> randomText(@RequestBody NumDTO numDto) {
        Set<Map<String, String>> content = readService.getRandomTexts(numDto.getNum());
        return new ResponseDTO<>("200", "success", content);
    }

    /**
     * 获取短句
     */
    @RequestMapping(value = "/sentence", method = RequestMethod.POST)
    public ResponseDTO<Object> getSentence(@RequestBody NumDTO numDto) {
        Map<String, String> content = readService.getSentence(numDto.getNum());
        return new ResponseDTO<>("200", "success", content);
    }

    /**
     * 随机获取一组短句
     */

    @RequestMapping(value = "/randomSentence", method = RequestMethod.POST)
    public ResponseDTO<Object> randomSentence(@RequestBody NumDTO numDto) {
        Set<Map<String, String>> content = readService.getRandomSentence(numDto.getNum());
        return new ResponseDTO<>("200", "success", content);
    }

    /**
     * 获取指定短文的所有评论
     */

    @RequestMapping(value = "/textComments", method = RequestMethod.POST)
    public ResponseDTO<Object> getComments(@RequestBody NumDTO numDto) {
        Map<Long, String> comments = readService.getTextComments(numDto.getNum());
        return new ResponseDTO<>("200", "success", comments);
    }

    /**
     * 给指定短文添加评论
     */

    @RequestMapping(value = "/commentsOnText", method = RequestMethod.POST)
    public ResponseDTO<Object> comments(@RequestBody CommentDTO commentDto) {
        String res = readService.commentOnText(commentDto.getNum(), commentDto.getContent());
        return new ResponseDTO<>("200", "success", res);
    }

    /**
     * 给指定短文点赞
     */

    @RequestMapping(value = "/likesText", method = RequestMethod.POST)
    public ResponseDTO<Object> textLikes(@RequestBody NumDTO numDto) {
        String likes = readService.likesOnText(numDto.getNum());
        return new ResponseDTO<>("200", "success", likes);
    }

    /**
     * 给指定短句点赞
     */

    @RequestMapping(value = "/likesSentence", method = RequestMethod.POST)
    public ResponseDTO<Object> sentenceLikes(@RequestBody NumDTO numDto) {
        String likes = readService.likesOnSentence(numDto.getNum());
        return new ResponseDTO<>("200", "success", likes);
    }

    /**
     * 初始化，将短文短句内容加入redis数据库
     */

    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public ResponseDTO<Object> init() {
        readService.initTextAndSentence();
        return new ResponseDTO<>("200", "success", null);
    }
}
